// 判断运行环境
export function checkBrowser() {
    if (/android/i.test(navigator.userAgent)) {
        return "android"
    }
    if (/ipad|iphone|mac/i.test(navigator.userAgent)) {
        return "ios"
    }
}

// 节流函数
export function throttle(fn, timeout) {
    let timer = null;
    return function(...args){
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.call(this, args);
        }, timeout);
    }()
}

// 防抖函数
export function debounce(fn, timeout) {
    let timer = null;
    let lastTime = null;
    return function(...args) {
        let nowTime = +new Date();
        if (lastTime && nowTime < lastTime + timeout) {
            clearTimeout(timer);
            timer = setTimeout(() => {
                fn.call(this, ...args);
                lastTime = nowTime;
            }, timeout);
        } else {
            fn.call(this, ...args);
            lastTime = nowTime;
        }
    }()
}
export default {
    checkBrowser,
    throttle,
    debounce
}